<?php

require_once 'smarty/Smarty.class.php';
require_once 'model_base.class.php';
require_once 'utils.class.php';
require_once 'backoffice_base.class.php';
require_once 'uagent_info.class.php';
require_once 'web.controller.class.php';
require_once 'web.view.class.php';

/**
*
* Class that starts the application
* Connecting with services
*
* @author Mauro Gadaleta
*
* @package web application
*/

class webApplication {
    /**
     * @var array
     */
	protected $errors = array();

    /**
     * @var null
     */
	public $service = null;

    /**
     * @var
     */
	public $view;

    /**
     * @var
     */
	public $controller;

    /**
     * @var
     */
	public $model;

    /**
     * @var array
     */
	public $params = array();

    /**
     * @var array
     */
  	public $externalParams = array();

    /**
     * @var 
     */
	public $cms;
	
    /**
     * Load the 3 services required for the operation of the website external service charge controllers. If there is any external controller
     * @return void
     */
	private function chargeElements(){
		$this->chargeModel();
		$this->chargeController();
		$this->chargeView();
		if($this->service!='index')
		{
			$this->chargeExternalControllers();
			$this->chargeExternalViews();
		}
    	$this->view->show();
	}

    /**
     * @return void
     */
	private function chargeExternalControllers() {
	    
	}

    /**
     * @return void
     */
	private function chargeExternalViews() {
	    
	}
	
    /**
     * Public function that starts the service chosen by the programmer
     * @param  $service
     * @return void
     */
	public function start($service) {
        $this->checkDevices();

        $install = $this->getInstallation();

        if(!$install)
        {
            if(!is_null($service) && $service!='')
            {
                $this->service = $service;
                $this->chargeElements();
            }
            else
            {
                $this->errors[] = 'No has cargado ningún servicio valido';
            }
        }
        else
        {
            $this->startInstallation();
        }

	}
	
    /**
     * Returns the service you need to start
     * @return null
     */
	public function getService() { return $this->service; }

    /**
     * Charge view
     * @return void
     */
	private function chargeView() {
		require_once $this->service.'.view.class.php';
		$className = $this->service.'View';
		$this->view = new $className($this->params);

        if(isset($_GET['action']))
        {
            $html = $this->view->$_GET['action']();
            if($_GET['ajax']) $this->view->assign('ajax',$html);
        }
	}

    /**
     * Charge controller
     * @return void
     */
	private function chargeController() {
		require_once $this->service.'.controller.class.php';
		$className = $this->service.'Controller';
		$this->controller = new $className($this->service);
        if(isset($_GET['action'])) $this->controller->$_GET['action']();
        $this->params = $this->controller->getParams();
	}

    /**
     * Charge model
     * @return void
     */
	private function chargeModel() {
		$this->model = new model;
	}
	
    /**
     * If there were errors, show on the screen
     * @return void
     */
	protected function showErrors() {
		utils::myprint_r($this->errors);
	}

    /**
     * Developing a module directly by CMS
     * @param  $module
     * @return void
     */
	public function startModule($module) {
		$this->chargeModel();
		$this->cms = new baseBackoffice($module);
	}

    /**
     * Instalación
     * @return bool
     */
    private function getInstallation() {
        if(is_dir('install'))
        {
            $con = Doctrine_Manager::connection();
            $tables = $con->import->listTables('MFConfiguration');

            if(!is_int(array_search('mf_configuration',$tables)))
                return true;
            else
                return false;
        }
        else
            return false;
    }

    /**
     * Empieza la instalación
     * @return void
     */
    private function startInstallation() {
        header("Location: install");
    }

    /**
     * Detección de Device (si es mobile redirige a mobile)
     * @return void
     */
    private function checkDevices() {
        $uagent_obj = new uagent_info();

        if($uagent_obj->detectMobile())
            utils::redirect('mobile/');

    }
}

?>